import { useRouter } from 'vue-router'

import { YkMessage } from '@yike-design/ui'

export function useCode() {
  const router = useRouter()

  // code验证
  const tackleCode = (e: number) => {
    if (e === 300) {
      // 需要登录
      YkMessage({ type: 'warning', message: 'token验证失效,请重新登陆' })
      router.push('/login')
    } else if (e === 400) {
      YkMessage({ type: 'warning', message: '请求参数不正确' })
      return false
    } else if (e === 401) {
      router.push('/register')
      return false
    } else if (e === 409) {
      return false
    } else {
      return true
    }
  }

  return { tackleCode }
}
